#-*- coding:utf-8 -*-

import re
import datetime
import copy
import sys

def get_dist(a,b):
    dist = 0

    ###过滤快乐星期天
    if len(a) > 7:
        a.pop()
    if len(b) > 7:
        b.pop()
    ###过滤快乐星期天

    if a.pop() == b.pop():
        dist+=1
    for i in a:
            for  j in b:
                    if i==j:
                            dist+=1
    
    return dist


#my_dist = int(sys.argv[1])

def get_lott_his():
    lott_his = []
    with open('lottery_his.txt','r') as f:
        lines = f.readlines()[1:]
        #print(len(lines))
        for line in lines:
            ball_list = []
            balls = re.split(',|\\\\',line.strip().split()[-1])
            #reds = balls.split(',\\')
            #for ball in reds:
            #   ball_list.append(ball)
            #blue = balls.split('\\')
            lott_his.append(map(int,balls))
    return(lott_his)
#   with open('lott_list.csv','w+') as wf:
#       for his in lott_his:
#           his_line = ''
#           for ball in his:
#               his_line += ball
#               his_line += ','
#           his_line = his_line.strip(',')
#           his_line +='\n'
#           wf.write(his_line)
def calc_his():
    start = datetime.datetime.now()
    a = 0
    dist_count = 0
    lenth = len(lott_his)   
    for i in range(lenth):
        for j in range(i+1,lenth):
            a+=1
            #if lott_his[i] ==lott_his[j]:
            #   print(lott_his[i])
            if a%100000 == 0:
                print(a)
            lott1 = copy.copy(lott_his[i])
            lott2 = copy.copy(lott_his[j])
            dist = get_dist(lott1,lott2)
            if dist <= my_dist:
                dist_count+=1
            #   print(lott_his[i]),
            #   print('||'),
            #   print(lott_his[j]),
            #   print('==>'),
            #   print(dist)
            #   print('\n')
        #   del lott1,lott2
    print(a)
    stop = datetime.datetime.now()
    last = (stop-start).seconds
    print('持续运行：'),
    print(last)

    print('匹配数量: '),
    print(dist_count)
    
    print('概率：'),
    print(float(dist_count)/float(a))

def filter():
    lott_his = get_lott_his()
    
    denominator = float(len(lott_his))
    
    file_handle = open('filter.txt','w+')
    
    for a in range(1,29):
        for b in range(a+1,30):
            for c in range(b+1,31):
                for d in range(c+1,32):
                    for e in range(d+1,33):
                        for f in range(e+1,34):
                            for g in range(1,17):
                                candicate = [a,b,c,d,e,f,g]
                                dist = 0
                                continue_flag = 0
                                for his in lott_his:
                                    cp1 = copy.copy(candicate)
                                    cp2 = copy.copy(his)
                                    this_dist = get_dist(cp1,cp2)
                                    if this_dist > 4:
                                        continue_flag = 1
                                        break
                                    dist += this_dist
                                if continue_flag ==1:
                                    continue
                                avg_dist = float(dist)/denominator

                                line=str(a)+','+str(b)+','+str(c)+','+str(d)+','+str(e)+','+str(f)+','+str(g)+' '+' '+str(avg_dist)+'\n'
                                
                                file_handle.write(line)



    file_handle.close()                                 


start = datetime.datetime.now()
print('start:'+start.strftime('%Y-%m-%d %H-%M-%s'))
filter()
end = datetime.datetime.now()
print('start:'+end.strftime('%Y-%m-%d %H-%M-%s'))

print((end-start).seconds)
